#ifndef RUNNER_H
#define RUNNER_H

#include <string>
#include <sys/stat.h>

#include "types.h"
#include "logger.h"

using std::string;

class Runner {
public:
    Runner(int sol_id, int rid, const char *dir, const char *fname, char assign_symbol);

    Runner(int sol_id, int rid, const char *fname, char assign_symbol);

    ~Runner();

private:
    int problem_id;
    string userid;
    LANG_TYPE lang;
    int solution_id;
    int time_lmt;
    int mem_lmt;

    int used_time;
    int used_mem;
    bool spj;
    int runner_id;
    string home_dir;

    string hostname;
    string username;
    string passwd;
    string dbname;
    string port;

    MYSQL *conn;

    JUDGE_RES_TYPE result;

    Logger log;

    void init_data(const configure &conf);
    bool init_mysql_conn();

    void get_solution_info();
    void get_problem_info();
    void get_solution();
    void update_solution(int result, int time, int memory);
    int compile();

    long get_file_size(const char *fname);
};

inline long
Runner::get_file_size(const char *fname)
{
    struct stat f_stat;
    if (stat(fname, &f_stat) == -1) return 0;
    return (long) f_stat.st_size;
}

#endif
